﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DrCarDrive.DBService;
using DrCarDrive.Wpf.WpfWindow;
using DrCarDrive.Util;

namespace DrCarDrive.Wpf.WpfPage
{
    /// <summary>
    /// Interaction logic for EndDrivePage.xaml
    /// </summary>
    public partial class EndDrivePage : Page
    {
        private static EndDrivePage instance;
        private DriveService driveService;
        
        public EndDrivePage()
        {
            InitializeComponent();
            driveService = new DriveService();
            tbl_drive td = new tbl_drive();
            this.DataContext = td;
        }

        public static EndDrivePage Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new EndDrivePage();
                }

      

                return instance;
            }
        }

        private void btnEndDrive_Click(object sender, RoutedEventArgs e)
        {
            if (validate() == true)
            {
                ConfirmationWindow cf = new ConfirmationWindow(Messages.TTL_END_DRIVE, Messages.MSG_END_DRIVE_CONF);

                cf.ShowDialog();

                if (ConfirmationWindow.isConfirm.Equals(MessageBoxResult.Yes))
                {
                    tbl_drive drive = SessionData.drive;
                    DateTime endDateTime = DateTime.ParseExact(dtpEndDateTime.Text, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
                    drive.END_DATE = endDateTime;
                    drive.END_ODOMETER = Double.Parse(txtEndOdometer.Text);
                    if (txtEndLocation.Text != "")
                    {
                        drive.TO = txtEndLocation.Text;
                    }
                    drive.STATUS = false;

                    driveService = new DriveService();
                    //MessageBox.Show();
                    int status = driveService.update(drive);
                    if (status > 0)
                    {
                        endDriveRecord();
                        MainWindow.Instance.ContentFrame.Content = DashboardPage.Instance;
                    }
                }                
            }
            else
            {
                MSGWindow errorWindow = new MSGWindow(Messages.TTL_FILL_REQUIRED, Messages.MSG_FILL_REQUIRED_INFORMATION);
                errorWindow.ShowDialog();
            }
        }

        private void endDriveRecord()
        {
            StartDrivePage.destroy();
            instance = null;
            SessionData.drive = null;
            SessionData.place = null;
            SessionData.totalPatients = null;
        }

        private Boolean validate()
        {
            if (txtEndOdometer.Text == "" || dtpEndDateTime.Value == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private void Page_Loaded_1(object sender, RoutedEventArgs e)
        {
            if (SessionData.drive != null)
            {
                txtEndLocation.Text = SessionData.drive.TO;
            }
        }

        private void txtEndOdometer_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = !IsNumberKey(e.Key) && !IsActionKey(e.Key);
        }
       
        private bool IsNumberKey(Key inKey)
        {
            if (inKey < Key.D0 || inKey > Key.D9)
            {
                if (inKey < Key.NumPad0 || inKey > Key.NumPad9)
                {
                    return false;
                }
            }
            return true;
        }

        private bool IsActionKey(Key inKey)
        {
            return inKey == Key.Delete || inKey == Key.Back || inKey == Key.Tab || inKey == Key.Return || Keyboard.Modifiers.HasFlag(ModifierKeys.Alt);
        }

        private void txtEndOdometer_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
    }
}